//
//  UserKeepArticleViewController.m
//  product_B
//
//  Created by lanou on 16/6/3.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "UserKeepArticleViewController.h"
#import "ReadMainModel.h"
#import "ReadserialModel.h"
#import "ReadQuestionModel.h"
#import "ReadTableViewController.h"
@interface UserKeepArticleViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *bigView;
@property (nonatomic,strong)UIView *smallView;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *esseyArray;
@property (nonatomic,strong)NSMutableArray *serialArray;
@property (nonatomic,strong)NSMutableArray *questionArray;
@property (nonatomic,strong)NSMutableArray *signArray;


@end

@implementation UserKeepArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.signArray = [@[@"1",@"6",@"2"]mutableCopy];
    [self requestWork];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, kWidth, kHeight +49 -64-50 )];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    self.bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 50)];
    self.bigView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"往期列表";
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(kWidth * 0.15, 10, 40, 25);
    [button1 setTitle:@"短篇" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1;
    [self.bigView addSubview:button1];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(kWidth * 0.45, 10, 40, 25);
    [button2 setTitle:@"连载" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 2;
    [self.bigView addSubview:button2];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(kWidth * 0.75, 10, 40, 25);
    [button3 setTitle:@"问题" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 3;
    self.smallView = [[UIView alloc]initWithFrame:CGRectMake(kWidth * 0.15, 47, 40, 2)];
    self.smallView.backgroundColor = [UIColor cyanColor];
    [self.bigView addSubview:self.smallView];
    [self.bigView addSubview:button3];
    [self.view addSubview:self.bigView];
    [self.view addSubview:self.tableView];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        if (_index == 0) {
            if (_esseyArray.count!=0) {
                ReadMainModel *model = _esseyArray.lastObject;
                [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/othercollection/%@/more/1/%@",_modelID,model.content_id] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
                    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                    NSMutableArray *array = [ReadMainModel readDetailModelWithConfigure:dic];
                    for (ReadMainModel *model in array) {
                        [_esseyArray addObject:model];
                    }
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                } errorBlock:nil];
            }
        }else if (_index == 1){
            if (_serialArray.count!=0) {
                ReadserialModel *model = _serialArray.lastObject;
                [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/othercollection/%@/more/6/%@",_modelID,model.myID] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
                    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                    NSMutableArray *array = [ReadserialModel readDetailSerialModelWithConfigure:dic];
                    for (ReadserialModel *model in array) {
                        [_serialArray addObject:model];
                    }
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                } errorBlock:nil];
            }
        }else{
            if (_questionArray.count!=0) {
                ReadQuestionModel *model = _questionArray.lastObject;
                [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/othercollection/%@/more/2/%@",_modelID,model.question_id] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
                    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                    NSMutableArray *array = [ReadQuestionModel readDetailQuestionModelWithConfigure:dic];
                    for (ReadQuestionModel *model in array) {
                        [_questionArray addObject:model];
                    }
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                    
                } errorBlock:nil];
            }
        }
    }];
}
- (void)requestWork{
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/othercollection/%@/more/%@/0",_modelID,self.signArray[_index]] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (_index == 0) {
            self.esseyArray = [ReadMainModel readDetailModelWithConfigure:dic];
        }else if (_index == 1){
            self.serialArray = [ReadserialModel readDetailSerialModelWithConfigure:dic];
        }else{
            self.questionArray = [ReadQuestionModel readDetailQuestionModelWithConfigure:dic];
        }
        [self.tableView reloadData];
    } errorBlock:^(NSError *error) {
        
    }];
}
- (void)buttonAction:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.smallView.frame = CGRectMake(sender.frame.origin.x, 47, 40, 2);
    }];
    self.index = sender.tag - 1;
    if (_index == 0) {
        if (_esseyArray.count == 0) {
            [self requestWork];
        }
    }else if (_index == 1){
        if (_serialArray.count == 0) {
            [self requestWork];
        }
    }else{
        if (_questionArray.count == 0) {
            [self requestWork];
        }
    }
    [self.tableView reloadData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_index == 0) {
        return _esseyArray.count;
    }else if (_index == 1){
        return _serialArray.count;
    }else{
        return _questionArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (_index == 0) {
        ReadMainModel *model = _esseyArray[indexPath.row];
        cell.textLabel.text = model.hp_title;
    }else if (_index == 1){
        ReadserialModel *model = _serialArray[indexPath.row];
        cell.textLabel.text = model.title;
    }else{
        ReadQuestionModel *model = _questionArray[indexPath.row];
        cell.textLabel.text = model.question_title;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
    if (_index == 0) {
        ReadMainModel *model = _esseyArray[indexPath.row];
        tableVC.modelID = model.content_id;
    }else if (_index == 1){
        ReadserialModel *model = _serialArray[indexPath.row];
        tableVC.modelID = model.myID;
    }else{
        ReadQuestionModel *model = _questionArray[indexPath.row];
        tableVC.modelID = model.question_id;
    }
    tableVC.indexY = _index;
    tableVC.isAdd = YES;
    [self.navigationController pushViewController:tableVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
